# https://mayanan.blog.csdn.net/article/details/116427427?spm=1001.2101.3001.6650.2&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_aa&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-2.pc_relevant_aa&utm_relevant_index=4

# python中当调用一个对象的 obj["name"] = "admin" 或者 obj["name"]的时候，说明该对象要么是字典，要么类中实现了__setitem__方法和__getitem__方法
# 一般类初始化obj(a,b,c)


class Foo(dict):
    pass

foo = Foo()
foo["name"] = "admin"

print(foo)


# class Foo():
#     pass
#
# foo = Foo()
# foo["name"] = "admin"                   # 如果没有继承dict会报错
#
# print(foo)


class Foo(object):
    def __init__(self):
        self._res = dict()

    def __getitem__(self, item):
        return self._res.get(item)

    def __setitem__(self, key, value):
        self._res[key] = value

    def __delitem__(self, key):
        print("__delitem__执行了")
        self._res.pop(key)


f = Foo()
f["123"] = 456  # 执行__setitem__方法
f["223"] = 789  # 执行__setitem__方法
print(f["223"])  # 执行__getitem__方法
del f["223"]  # 删除前执行__delitem__方法
print(f.__dict__)
